//
//  CYOriginalStatusView.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/9.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

let CYStatusCellMargin: CGFloat = 8




class CYOriginalStatusView: UIView {
    
    var bottomConstraint: Constraint?

    var statusViewModel: CYStatusViewModel? {
        didSet {
            if let urlString = statusViewModel?.status?.user?.profile_image_url, let url = NSURL(string: urlString) {
                headImage.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
            }
//            nameLabel.text = statusViewModel?.status?.user?.screen_name

            nameLabel.text = "媳妇"
            
            timeLabel.text = statusViewModel?.status?.created_at
            
            sourceLabel.text = statusViewModel?.sourceText
            
            verifienImage.image = statusViewModel?.verifiedImage
            
            membershipImage.image = statusViewModel?.membershipImage
            
            contentLabel.text = statusViewModel?.status?.text
            
            self.bottomConstraint?.uninstall()
//            self.constra
            if let picUrl = statusViewModel?.status?.pic_urls where picUrl.count > 0 {
                
               pictureView.picUrls = picUrl
                
                pictureView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(CYStatusCellMargin).constraint
                })
            }else {
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(CYStatusCellMargin).constraint
                })
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifienImage)
        addSubview(membershipImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImage.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(CYStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage.snp_trailing).offset(CYStatusCellMargin)
            make.top.equalTo(headImage)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImage)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing)
            make.top.equalTo(timeLabel)
        }
        
        verifienImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_trailing).offset(-3)
            make.centerY.equalTo(headImage.snp_bottom).offset(-3)
        }
        
        membershipImage.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CYStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(CYStatusCellMargin)
            make.trailing.equalTo(self).offset(-CYStatusCellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(CYStatusCellMargin)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint = make.bottom.equalTo(pictureView).offset(CYStatusCellMargin).constraint
        }
        
    }
    
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "IMG_2494"))
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
       label.text = "毛毛"
        
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
       let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
        label.text = "一年前"
        return label
        
    }()
    
    private lazy var sourceLabel: UILabel = {
       let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
        label.text = "  来自 Nokia塞班系统"
        return label
    }()
    
    private lazy var verifienImage: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "avatar_vip")
        
        return imageView
    }()
    
    private lazy var membershipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView: CYStatusPictureView = CYStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
}
